package com.neuedu.demoweb.service.impl;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.neuedu.demoweb.bean.Admin;
import com.neuedu.demoweb.common.Pager;
import com.neuedu.demoweb.common.WebException;
import com.neuedu.demoweb.dao.AdminDao;
import com.neuedu.demoweb.dao.impl.AdminDaoImpl;
import com.neuedu.demoweb.service.AdminService;
import com.neuedu.demoweb.utils.DbUtil;


/**
 * 
 * @author chens
 *
 */
public class AdminServiceImpl implements AdminService {
	private AdminDao adminDao = null;

	public AdminServiceImpl() {
		this.adminDao = new AdminDaoImpl();
	}

	@Override
	public boolean add(Admin object) throws WebException {
		boolean ret = false;
		Connection conn = DbUtil.openConnection();
		DbUtil.beginTransation(conn);
		try {
			// 把Connection连接传递到dao层，事务控制必须共用同一个Connection连接
			adminDao.setConnection(conn);
			ret = adminDao.add(object);
			if (ret) {
				DbUtil.commitTransation(conn);
			} else {
				DbUtil.rollblackTransation(conn);
			}
		} catch (WebException e) {
			DbUtil.rollblackTransation(conn);
			// 继续向控制层抛出异常，
			throw e;
		} finally {
			DbUtil.close(conn, null, null);
		}
		return ret;
	}

	@Override
	public boolean edit(Admin object) throws WebException {

		boolean ret = false;
		Connection conn = DbUtil.openConnection();
		DbUtil.beginTransation(conn);
		try {
			adminDao.setConnection(conn);
			ret = adminDao.edit(object);
			if (ret) {
				DbUtil.commitTransation(conn);
			} else {
				DbUtil.rollblackTransation(conn);
			}
		} catch (WebException e) {
			DbUtil.rollblackTransation(conn);
			throw e;
		} finally {
			DbUtil.close(conn, null, null);
		}
		return ret;
	}

	@Override
	public boolean deleteById(Integer id) throws WebException {

		boolean ret = false;
		Connection conn = DbUtil.openConnection();
		DbUtil.beginTransation(conn);
		try {
			adminDao.setConnection(conn);
			ret = adminDao.deleteById(id);
			if (ret) {
				DbUtil.commitTransation(conn);
			} else {
				DbUtil.rollblackTransation(conn);
			}
		} catch (WebException e) {
			DbUtil.rollblackTransation(conn);
			throw e;
		} finally {
			DbUtil.close(conn, null, null);
		}
		return ret;
	}

	@Override
	public List<Admin> findList() throws WebException {
		List<Admin> list = new ArrayList<Admin>();

		Connection conn = DbUtil.openConnection();
		try {
			adminDao.setConnection(conn);
			list = adminDao.findList();
		} catch (WebException e) {
			throw e;
		} finally {
			DbUtil.close(conn, null, null);
		}

		return list;
	}

	@Override
	public Admin findById(Integer id) throws WebException {
		Admin admin = null;
		Connection conn = DbUtil.openConnection();
		try {
			adminDao.setConnection(conn);
			admin = adminDao.findById(id);
		} catch (WebException e) {
			throw e;
		} finally {
			DbUtil.close(conn, null, null);
		}
		return admin;
	}

	@Override
	public List<Admin> search(Pager<Admin> pager) throws WebException {

		List<Admin> list = new ArrayList<Admin>();
		Connection conn = DbUtil.openConnection();
		try {
			adminDao.setConnection(conn);
			list = adminDao.search(pager);
		} catch (WebException e) {
			throw e;
		} finally {
			DbUtil.close(conn, null, null);
		}

		return list;
	}

	@Override
	public int findTotal(Pager<Admin> pager) throws WebException {
		int total = 0;

		Connection conn = DbUtil.openConnection();
		try {
			adminDao.setConnection(conn);
			total = adminDao.findTotal(pager);
		} catch (WebException e) {
			throw e;
		} finally {
			DbUtil.close(conn, null, null);
		}

		return total;
	}

	@Override
	public Admin login(Admin admin) throws WebException {
		Admin tempAdmin = null;
		Connection conn = DbUtil.openConnection();
		try {
			adminDao.setConnection(conn);
			tempAdmin = adminDao.login(admin);
		} catch (WebException e) {
			throw e;
		} finally {
			DbUtil.close(conn, null, null);
		}
		return tempAdmin;
	}

}
